package com.zhiyou100.crm.dao.impl;

import com.zhiyou100.crm.dao.CustomerSourceDao;
import com.zhiyou100.crm.util.CustomerSource;
import com.zhiyou100.crm.util.DBUtil1;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class CustomerSourceDaoImpl implements CustomerSourceDao {
    DBUtil1 dbUtil1=new DBUtil1();
    ResultSet rs;
    @Override
    public List<CustomerSource> qureyAll() {
        List<CustomerSource> customerSources=new ArrayList<>();
        String sql="select * from customer_source";
        rs=dbUtil1.select(sql,null);
        try {
            while (rs.next()) {
                int customer_source_id = rs.getInt("customer_source_id");
                String customer_source_name = rs.getString("customer_source_name");
                String customer_source_desc = rs.getString("customer_source_desc");
                String remark = rs.getString("remark");
                Timestamp create_time = rs.getTimestamp("create_time");
                Timestamp update_time = rs.getTimestamp("update_time");
                int status = rs.getInt("status");
                int creater = rs.getInt("creater");
                int updater = rs.getInt("updater");
                CustomerSource customerSource=new CustomerSource(customer_source_id,customer_source_name,customer_source_desc,status,remark,create_time,creater,update_time,updater);
                customerSources.add(customerSource);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }finally {
            dbUtil1.close();
        }
        return customerSources;
    }

    @Override
    public int delete(int id) {
        String sql="delete from customer_source where customer_source_id=?";
        Object[] obj={id};
        return dbUtil1.update(sql,obj);
    }

    @Override
    public boolean add(CustomerSource customerSource) {
        String sql="insert into customer_source(customer_source_name,customer_source_desc,create_time,creater) values(?,?,?,?)";
        Object[] obj={customerSource.getCustomer_source_name(),customerSource.getCustomer_source_desc(),customerSource.getCreate_time(),customerSource.getCreater()};
        int a=dbUtil1.update(sql,obj);
        if(a>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int update(CustomerSource customerSource) {
        String sql="update customer_source set customer_source_name=?,customer_source_desc=?,update_time=?,updater=? where customer_source_id=?";
        Object[] obj={customerSource.getCustomer_source_name(),customerSource.getCustomer_source_desc(),customerSource.getUpdate_time(),customerSource.getUpdater()};
        return dbUtil1.update(sql,obj);
    }
}
